package pun.potato;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Window;
import android.widget.TextView;

public class MainMenu extends FragmentActivity {

	private MainFragment mainFragment;
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainmenu);
		
		TextView topicPlay = (TextView)findViewById(R.id.Pundamodium);
		Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/hotdog.ttf");
		topicPlay.setTypeface(tf);
		
		if(savedInstanceState == null){
			//add the fragment on initial activity setup
			mainFragment = new MainFragment();
			getSupportFragmentManager()
			.beginTransaction()
			.add(android.R.id.content, mainFragment)
			.commit();
		}
		else{
			//or set the fragment from restored state info
			mainFragment = (MainFragment)getSupportFragmentManager()
					.findFragmentById(android.R.id.content);
		}
		  
	}


	
	

	@Override
	protected void onPause() {
		super.onPause();
		mainFragment.checkFriendGame = false;
		new ResetWaitingForFriend().execute("start");
	}





	@Override
	public void onBackPressed() {
		super.onBackPressed();
		mainFragment.checkFriendGame = false;
		new ResetWaitingForFriend().execute("start");
		MainMenu.this.finish();
		
	}
	
	class ResetWaitingForFriend extends AsyncTask<String, String, String>{

		@Override
		protected String doInBackground(String... params) {
			
			HttpClient client = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(Preferences.GetURL() + "resetAfterTimeout.php?id=" + mainFragment.getUserId() + Preferences.GetLiveServerPreferences());
			
			try{
				HttpResponse response = client.execute(httpPost);
				String jsonString = EntityUtils.toString(response.getEntity());
				//Log.d("ResetAfterTimeout", jsonString);
			}catch(IOException e){
				e.printStackTrace();
			}
			
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
		}
		
		
		
	}
	
	
	
	
	

}
